package com.lihui.dao;

import com.lihui.model.Course;
import com.lihui.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2022/12/29  16:44
 */
public class CourseDao {
    public List<Course> selectByName(String courseName){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Course> result = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "select * from course where name = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,courseName);
            resultSet = statement.executeQuery();
            while(resultSet.next()){
                if(result == null){
                    result = new ArrayList<>();
                }
                Course course = new Course();
                course.setId(resultSet.getInt(1));
                course.setName(resultSet.getString(2));
                result.add(course);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet,statement,connection);
        }
        return result;
    }
}
